package com.bytecollege.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.bytecollege.model.ArticleCollect;
import com.bytecollege.vo.ArticleCollectVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName: CollectMapper
 * @Description:
 * @Author: Sun
 * @Date: 2022/10/18 21:03
 */
@Mapper
public interface ArticleCollectMapper extends BaseMapper<ArticleCollect> {

    /**
     * 获取用户收藏的文章列表
     * @param page
     * @param userId
     * @return
     */

    @Select("select a.User_Id,a.Article_Id,Article_title,Article_content,Article_clicks,AddTime,Collect_Id from articlecollect left join article a on articlecollect.Article_id = a.Article_Id left join user u on a.User_Id = u.User_Id where  a.User_Id =#{userId}")
    List<ArticleCollectVo> getArticleCollect(Page page, Integer userId);

    /**
     * 获取帖子对应的收藏量
     * @param articleId
     * @return
     */
    @Select("select count(collect_id) from articlecollect where article_id=#{articleId} group by article_id")
    Integer getCollectNumByArticleId(Integer articleId);


    /**
     * 查询单个文章获取收藏状态
     * @param wrapper
     * @return
     */
    @Select("select collect_id,article_id,user_id,addtime from articlecollect ${ew.customSqlSegment} group by article_id")
    ArticleCollect getOneArticle(@Param("ew") Wrapper wrapper);

    /**
     * 添加文章收藏
     * @param articeCollect
     * @return
     */
    @Insert("insert into articlecollect(article_id,user_id,addtime) values(#{articleId},#{userId},#{addtime})")
    int addCollect(ArticleCollect articeCollect);


    @Delete("delete from articlecollect where Collect_Id = #{id} ")
    Integer del(Integer id);
}